# vPresent is an immersive presentation creation and display application.
# Copyright (C) 2007-2009 by Priority 5 Holdings, Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import opendocument
import opendocument.xlink as xlink

od = opendocument
AI = opendocument.AttributeInfo
text_base = opendocument.bases.text

#class TextContainer(opendocument.DocumentNode):
#   def __init__(self, attrs = None, childTypes = None):
#      opendocument.DocumentNode.__init__(self, attrs, childTypes)

#class StyledTextContainer(TextContainer):
#   '''
#   Base class for content that holds text.  In XML terms, these represent
#   elements with mixed complex content (both text and other elements).  These
#   document nodes have attributes--in particular text:style-name.
#   '''
#   def __init__(self, attrs = None, childTypes = None):
#      child_types = [TextContainer]
#
#      if childTypes is not None:
#	 if not isinatance(childTypes, list):
#	    childTypes = [childTypes]
#
#	 child_types = child_types + childTypes
#
#      TextContainer.__init__(self, attrs, child_types)
#      self.textStyleName = attrs.get('text:style-name', '')
#
#   def getPlainContent(self):
#      if self._children is not None:
#         text = []
#         for c in self._children:
#            text.append(c.getContent())
#
#         return ' '.join(text)
#      else:
#         return ''
#
#   def getTextStyleName(self):
#      return self.textStyleName

#class RawTextContainer(TextContainer):
#   '''
#   Base class for 
#   '''
#   def __init__(self, attrs, content):
#      TextContainer.__init__(self, attrs)
#      self.content = content
#
#   def getContent(self):
#      return self.content

#def isParagraphContent(child):
#   return isinstance(child, opendocument.PlainText) or isinstance(child, Span) or \
#          isinstance(child, Hyperlink) or isinstance(child, Space)

#class Paragraph(StyledTextContainer):
#   def __init__(self, attrs):
#      StyledTextContainer.__init__(self, attrs)
#
#      # Restrict the child types for paragraphs.
#      self.childTypes = [isParagraphContent]
#
#      self.id = attrs.get('text:id', '')
#      self.condStyleName = attrs.get('text:cond-style-name', '')
#      class_names = attrs.get('text:class-names', '')
#
#      if class_names is not None and len(class_names) > 0:
#         self.classNames = class_names.split(' ')
#      else:
#         self.classNames = []
#
#   def getID(self):
#      return self.id
#
#   def getCondStyleName(self):
#      return self.condStyleName
#
#   def getClassNames(self):
#      return self.classNames

# NOTE: Strictly speaking, a heading is not exactly a paragraph, but it has
# quite a lot in common with one. In this context, this class structure should
# be fine.
#class Heading(Paragraph):
#   def __init__(self, attrs):
#      Paragraph.__init__(self, attrs)
#      self.addChildType(Number)
#
#      try:
#         self.outlineLevel = int(attrs.get('text:outline-level', 1))
#         if self.outlineLevel <= 0:
#            self.outlineLevel = 1
#      except ValueError:
#         self.outlineLevel = 1
#
#      restart_numbering = attrs.get('text:restart-numbering', 'false')
#
#      if restart_numbering.lower() == 'true':
#         self.restartNumbering = True
#      else:
#         self.restartNumbering = False
#
#      try:
#         self.startValue = int(attrs.get('text:start-value', 1))
#         if self.startValue < 0:
#            self.startValue = 1
#      except ValueError:
#         self.startValue = 1
#
#      list_header = attrs.get('text:is-list-header', 'false')
#      if list_header.lower() == 'true':
#         self.listHeader = True
#      else:
#         self.listHeader = False
#
#   def getOutlineLevel(self):
#      return self.outlineLevel
#
#   def shouldRestartNumbering(self):
#      return self.restartNumbering
#
#   def getStartValue(self):
#      return self.startValue
#
#   def isListHeader(self):
#      return self.listHeader

class List(text_base.List):
   __metaclass__ = od.OpenDocumentMeta
   overrides = {'text:continue-numbering': AI(default='false')}

   def __init__(self, attrs):
      text_base.List.__init__(self, attrs)

   #def shouldContinueNumbering(self):
   #   return self.continueNumbering

   #def getItems(self):
   #   return self.getChildren()

   #def getNumItems(self):
   #   return self.getNumChildren()

#class Span(StyledTextContainer):
#   def __init__(self, attrs):
#      StyledTextContainer.__init__(self, attrs)
#      self.childTypes = [RawTextContainer]

#class Number(RawTextContainer):
#   def __init__(self, content):
#      RawTextContainer.__init__(self, None, content)

#class Character(opendocument.DocumentNode):
#   '''
#   Base class for representation of a character.
#   '''
#
#   def __init__(self, attrs, character = None):
#      opendocument.DocumentNode.__init__(self, attrs)
#      self.character = character
#
#   def getCharacter(self):
#      return self.character

#class Space(Character):
#   def __init__(self, attrs):
#      Character.__init__(self, attrs)
#
#      try:
#         self.character = ord(attrs.get('text:c', u' '))
#      except ValueError:
#         self.character = ord(u' ')

#class Tab(Character):
#   def __init__(self, attrs):
#      Character.__init__(self, attrs)
#
#      try:
#         self.tabRef = int(attrs.get('text:tab-ref', 0))
#         if self.tabRef < 0:
#            self.tabRef = 0
#      except ValueError:
#         self.tabRef = 0
#
#   def getCharacter(self):
#      return u'\t'
#
#   def getTabRef(self):
#      return self.tabRef

#class LineBreak(Character):
#   def __init__(self, attrs):
#      Character.__init__(self, attrs)

#class SoftPageBreak(Character):
#   def __init__(self, attrs):
#      Character.__init__(self, attrs)

class Hyperlink(text_base.A):
   def __init__(self, attrs):
      StyledTextContainer.__init__(self, attrs)

      self._link             = xlink.XLink(attrs)
      self._eventListeners = None

      if not attrs.has_key('xlink:show'):
         self.__setShowFromFrame()

   def __setShowFromFrame(self):
      if self.targetFrame == '_blank':
         self.link.setShow('new')
      else:
         self.link.setShow('replace')

   #def apply(self, visitor):
   #   StyledTextContainer.apply(self, visitor)
   #
   #   if self.eventListeners is not None:
   #      self.eventListeners.apply(visitor)

   #      for c in self.eventListeners.getChildren():
   #         c.apply(visitor)

   #      visitor.closeContainer()
